//
//  XMGMessageTVc.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGMessageTVc.h"
#import "XMGMessageCell.h"
#import "XMGTopCell.h"

#import "XMGTopCellSelectedView.h"
#import "XMGFoundInMessageVC.h"
#import "XMGChatView.h"
@interface XMGMessageTVc ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UIGestureRecognizerDelegate>
@property (weak, nonatomic) UISearchBar *searchB;
@property (assign, nonatomic) NSInteger count;
@property (strong, nonatomic) XMGTopCellSelectedView *selectedView;
@property (strong, nonatomic) XMGChatView *chatView;

@end

@implementation XMGMessageTVc
static NSString *ID = @"cell";
static NSString *TOP = @"top";
- (XMGTopCellSelectedView *)selectedView {
    
    if (!_selectedView) {
        
      XMGTopCellSelectedView *selectedView = [[XMGTopCellSelectedView alloc]init];
        
        self.selectedView = selectedView;
    }
    
    return _selectedView;
    
}


- (XMGChatView *)chatView {
    if (!_chatView) {
        
        XMGChatView *chatView = [[XMGChatView alloc]init];
  
        [self.tableView addSubview:chatView];
        
        self.chatView = chatView;
        
    }

    return _chatView;
}



- (void)viewDidLoad {
 [super viewDidLoad];
    
#pragma mark - 自定义导航栏上的barButtonItem
    UIButton *btnL = [[UIButton alloc]init];
    
    [btnL setTitle:@"发现群" forState:UIControlStateNormal];
    
    [btnL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [btnL setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    [btnL sizeToFit];
    
    [btnL addTarget:self action:@selector(found) forControlEvents:UIControlEventTouchUpInside];
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnL];

    
    
    
    UIButton *btn =  [[UIButton alloc] init];
    
    [btn setImage:[UIImage imageNamed:@"聊天"] forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"聊天hight"] forState:UIControlStateHighlighted];
 
    [btn addTarget:self action:@selector(chat) forControlEvents:UIControlEventTouchUpInside];
    
    [btn sizeToFit];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    
    self.navigationItem.title = @"消息";
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGMessageCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTopCell class]) bundle:nil] forCellReuseIdentifier:TOP];
    
    self.tableView.rowHeight = 64;

#pragma mark - 添加搜索栏
    UISearchBar *serB = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 375, 44)];

    self.tableView.tableHeaderView = serB;

    [serB setContentMode:UIViewContentModeLeft];

    serB.placeholder = @"搜索联系人和群                                                          ";
    
    self.searchB = serB;
    
   
    

//    WBAccount *account = [GetWeiboAccount getAccount];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    tap.delegate = self;
    
    [self.tableView addGestureRecognizer:tap];
    
    
}




- (void)tap {
    
    
    self.chatView.alpha = 0;
    
    self.count = 0;
    
    [self.searchB resignFirstResponder];
    
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}



#pragma mark - 跳转发现群界面
- (void)found {
    
    XMGFoundInMessageVC *foundVC = [[XMGFoundInMessageVC alloc]init];
    
    foundVC.title = @"发现群";
    
    [self.navigationController pushViewController:foundVC animated:YES];
    
}

#pragma mark - 跳转聊天界面
- (void)chat {
    
    self.chatView.alpha = 1;
    
    self.count++;
    
    NSLog(@"%li",self.count);
    
    if (self.count % 2 == 0) {
        self.chatView.hidden = YES;
    }else  {
        
        
        self.chatView.hidden = NO;
        
    }
 
 
}

- (void)viewWillDisappear:(BOOL)animated {
    
    
    self.chatView.hidden = YES;
    
    self.count = 0;
    
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        
        return 4;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < 4 ) {
        
        XMGTopCell *cell1 = [tableView dequeueReusableCellWithIdentifier:TOP];
       
        switch (indexPath.row) {
            case 0:
                cell1.textL.text = @"@我的";

                cell1.imageV.image = [UIImage imageNamed:@"messagescenter_at"];
                
                break;
            case 1:
                cell1.textL.text = @"评论";
                
                cell1.imageV.image = [UIImage imageNamed:@"messagescenter_comments"];
                break;
            case 2:
                cell1.textL.text = @"赞";
                
                cell1.imageV.image = [UIImage imageNamed:@"messagescenter_good"];
                break;
            case 3:
                cell1.textL.text = @"订阅消息";
                
                cell1.imageV.image = [UIImage imageNamed:@"messagescenter_subscription"];
                break;
                
            default:
                break;
        }
        
        
        return cell1;
    }else {
        
        XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        return cell;
        
    }

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [self.searchB resignFirstResponder];
    
    self.chatView.hidden = YES;
    
    
    if (indexPath.row < 4) {

        XMGTopCellSelectedView *topvc = [[XMGTopCellSelectedView alloc] init];
        
        switch (indexPath.row) {
            case 0:
                topvc.title = @"所有微博";
          
                break;
            case 1:
               topvc.title = @"所有评论";
         
                break;
            case 2:
               topvc.title = @"赞";

                break;
            case 3:
                topvc.title = @"订阅消息";

                break;
                
                
            default:
                break;
        }
        
        [self.navigationController pushViewController:topvc animated:YES];
  
    }
 
    
}


@end
